<template>
	<view class="moreAppointmentContainer">
		<view class="headNavBox flex">
			<view class="headNavItem" v-for="item,index in navList" :key="index" @click="changeTab(index)">
				<view class="headNavTitle">{{item.title}}</view>
				<view :class="navIdx===index ? 'activeTab' : ''" :style="{'margin-left': item.marginLeftNavBottom + '%' }"></view>
			</view>
		</view>
		<view v-if="classrooms && classrooms.length > 0"  style="background: white;padding: 10px;display: flex;justify-content: space-between;margin-top: 5px;">
			<picker style="flex: 1;" mode="selector" :range="classrooms.map(v=>v.laboratoryName)" @change="bindPickerChange">
			  <view class="picker">{{classrooms[selectedIndex].laboratoryName}}</view>
			</picker>
			<image src="../../static/images/moreIconDown.png" class="moreIconHui"></image>
		</view>
		<div
		    style="display: -webkit-flex;display: flex;align-items:center; align-content:center;justify-content:center;margin-top: 10px;">
		    <button type="primary" size="small" @click="preWeek" style="font-size: 12px;background: linear-gradient(90deg, #19AD78, #1EBE85);">上一周</button>
		    <div style="text-align:center;font-size: 14px;line-height: 14px;font-weight: bold;">当前日期：{{ currentDay }}</div>
		    <button type="primary" size="small" @click="nextWeek" style="font-size: 12px;background: linear-gradient(90deg, #19AD78, #1EBE85);">下一周</button>
		</div>
		
			
		<scroll-view :scroll-y="isScroll" :style="{ height: windowHeight + 'px' }" @scrolltolower="lower" :scroll-top="scrollTop">
			<div class='class-table'>
			    <div class='table-wrapper'>
			        <div class='tabel-container'>
			            <table>
			                <thead>
			                    <tr>
			                        <th>时间</th>
			                        <th v-for='(item, index) in dateTimeList' :key='index'>
			                            <p>{{ item.week }}</p>
			                            <p class='period'>{{ item.date }}</p>
			                        </th>
			                    </tr>
			                </thead>
			                <tbody v-if="timeList.length != 0">
			                    <tr v-for='(temp, jdx) in timeList' :key='jdx'>
			                        <td>
			                            <p>{{ temp.timeSt }}</p>
			                            <p>{{ temp.timeEn }}</p>
			                        </td>
									<td v-for='(item, index) in dateTimeList' :key='index'>
									    <view class="mainRUl-li tipsOrange"
									    	v-if="appointmentList.find(v=>v.appointmentDate.indexOf(item.date)>0 && v.laboratoryId === classrooms[selectedIndex].laboratoryId && v.orderDetailList.find(x=>x.startTime === temp.timeSt))"
											style="font-size: 12px;color:white;"
											@click="jumpInfo(appointmentList.find(v=>v.appointmentDate.indexOf(item.date)>0&& v.laboratoryId === classrooms[selectedIndex].laboratoryId && v.orderDetailList.find(x=>x.startTime === temp.timeSt)).orderId)"
											>
											{{appointmentList.find(v=>v.appointmentDate.indexOf(item.date)>0&& v.laboratoryId === classrooms[selectedIndex].laboratoryId && v.orderDetailList.find(x=>x.startTime === temp.timeSt)).laboratoryName}}
											</view>
									    <view class="mainRUl-li tipsHui"
									    	v-else></view>
									</td>
			                    </tr>
			                </tbody>
			                <tbody v-else>
			                    <td colspan="8">
			                        <el-empty :image-size="200"></el-empty>
			                    </td>
			                </tbody>
			            </table>
			        </div>
			    </div>
			</div>
			
			<block v-if="appointmentList.filter(v=>v.laboratoryId === classrooms[selectedIndex].laboratoryId).length > 0">
				<view class="appointmentItem" v-for="item,index in appointmentList.filter(v=>v.laboratoryId === classrooms[selectedIndex].laboratoryId)" :key="index" @click="jumpInfo(item.orderId)">
					<view class="appointmentHead flex">
						<image src="../../static/images/classRoomIcon.png" class="classRoomIcon"></image>
						<view class="appointmentTit txtOverFlow">{{item.laboratoryName}}</view>
						<view class="appointmentTime" v-if="item.orderState==0">待审核</view>
						<view class="appointmentTime" v-if="item.orderState==10">审核通过</view>
						<view class="appointmentTime" v-if="item.orderState==20 && item.isSigned==0 && item.isOut==0">待签到</view>
						<view class="appointmentTime" v-if="item.orderState==20 && item.isSigned==1 && item.isOut==0">进行中</view>
						<view class="appointmentTime" v-if="item.orderState==20 && item.isSigned==1 && item.isOut==1">已签退</view>
						<view class="appointmentTime" v-if="item.orderState==30">已完成</view>
						<view class="appointmentTime">{{item.appointmentDate}}</view>
					</view>
					<view class="appointmentTimeBox" v-if="item.isShare != 1">
						<block v-for="temp,jdex in item.orderDetailList" :key="jdex" >
							<view class="appointmentTimeItem">{{temp.startTime}}-{{temp.endTime}}</view>
						</block>
					</view>
					<view class="appointmentLocationBox flex" v-if="item.roomLocation">
						<image src="../../static/images/locationIcon.png" class="locationIcon"></image>
						<view class="locationTxt">{{item.roomLocation}}</view>
					</view>
					<!-- 1.在订单列如果订单状态是（10）则在订单列表界面显示 取消 -->
					<view class="orderBtn" v-if="item.orderState == 10" @click.stop="cancleOrder(item)">取消</view>
				</view>
			</block>
			<view v-else class="noDataTxt">暂无数据</view>
		</scroll-view>
	</view>
</template>

<script>
import utils from '../../utils/utils';
	export default {
		data() {
			return {
				navIdx: 0,
				selectedIndex: 0,
				navList: [
					{
						title: '全部预约',
						id:0,
						checked: true,
						marginLeftNavBottom: 41
					},{
						title: '我预约的',
						id:1,
						checked: false,
						marginLeftNavBottom: 41
					},{
						title: '邀请我的',
						id:2,
						checked: false,
						marginLeftNavBottom: 41
					}
				],
				active: 0,
				appointmentList: [],
				isVisited: '',
				page: 1,
				limit: 999,
				isScroll: true,
				windowHeight: 0,
				scrollTop: 0,
				hasMoreData: false,//是否还有更多数据
				timeList: [
					  {
						"timeSt": "08:00",
						"timeEn": "09:00",
						"invalid": null,
						"used": 0,
						"siteId": null,
						"isOpen": 0,
						"orderDetailList": null
					  },
					  {
						"timeSt": "08:50",
						"timeEn": "09:50",
						"invalid": null,
						"used": 0,
						"siteId": null,
						"isOpen": 0,
						"orderDetailList": null
					  },
					  {
						"timeSt": "09:55",
						"timeEn": "10:55",
						"invalid": null,
						"used": 0,
						"siteId": null,
						"isOpen": 0,
						"orderDetailList": null
					  },
					  {
						"timeSt": "10:45",
						"timeEn": "11:45",
						"invalid": null,
						"used": 0,
						"siteId": null,
						"isOpen": 0,
						"orderDetailList": null
					  },
					  {
						"timeSt": "13:30",
						"timeEn": "14:30",
						"invalid": null,
						"used": 0,
						"siteId": null,
						"isOpen": 0,
						"orderDetailList": null
					  },
					  {
						"timeSt": "14:20",
						"timeEn": "15:20",
						"invalid": null,
						"used": 0,
						"siteId": null,
						"isOpen": 0,
						"orderDetailList": null
					  },
					  {
						"timeSt": "15:25",
						"timeEn": "16:25",
						"invalid": null,
						"used": 0,
						"siteId": null,
						"isOpen": 0,
						"orderDetailList": null
					  },
					  {
						"timeSt": "16:15",
						"timeEn": "17:15",
						"invalid": null,
						"used": 0,
						"siteId": null,
						"isOpen": 0,
						"orderDetailList": null
					  },
					  {
						"timeSt": "18:00",
						"timeEn": "19:00",
						"invalid": null,
						"used": 0,
						"siteId": null,
						"isOpen": 0,
						"orderDetailList": null
					  },
					  {
						"timeSt": "18:50",
						"timeEn": "19:50",
						"invalid": null,
						"used": 0,
						"siteId": null,
						"isOpen": 0,
						"orderDetailList": null
					  },
					  {
						"timeSt": "19:55",
						"timeEn": "20:55",
						"invalid": null,
						"used": 0,
						"siteId": null,
						"isOpen": 0,
						"orderDetailList": null
					  },
					  {
						"timeSt": "20:45",
						"timeEn": "21:45",
						"invalid": null,
						"used": 0,
						"siteId": null,
						"isOpen": 0,
						"orderDetailList": null
					  }
					],
				dateTimeList: utils.getCurrentWeek(),
				week: 0,
				currentDay: utils.getCurrDate(),
				classrooms: []
			}
		},
		onLoad() {
			let _this = this;
			wx.getSystemInfo({
				success: function(res) {
					_this.windowHeight = res.windowHeight;
				}
			})
			const w = utils.getCurrentWeek();
			console.log(w);
		},
		onShow() {
			this.scrollTop = 0;
			this.page = 1;
			this.appointmentList = [];
			this.searchAppointmentList();
		},
		methods: {
			jumpInfo(orderId) {
				uni.navigateTo({
					url: '/pages/appointmentInfo/appointmentInfo?orderId='+orderId
				});
			},
			preWeek() {
			    this.week = this.week - 1
			    this.dateTimeList = utils.getCurrentWeek(this.week);
			},
			nextWeek() {
			    this.week = this.week + 1
			    this.dateTimeList=  utils.getCurrentWeek(this.week);
			},
			//切换tab页
			changeTab(idx) {
				this.navIdx = idx;
				if(idx == 2) {
					this.isVisited = 1; //是否只查询被邀请的预约单据 0否；1是
				}else if(idx == 1) {
					this.isVisited = 0;
				}else {
					this.isVisited = '';
				}
				this.searchAppointmentList(1);
			},
			bindPickerChange(e) {
				this.selectedIndex = e.target.value;
				const that = this
				this.queryTimeList(this.classrooms[this.selectedIndex].laboratoryId)
				
			},
			queryTimeList(laboratoryId) {
				this.$utils.requestFun('/curriculum/curriculumStudy/api/getBackCourseScheduleByWeek', {
				        token: getApp().globalData.token,
				        classroomId: laboratoryId,
						// week: this.week
				    })
				    .then(res => {
				        const tableData = res.data.data
				       const timeList = [];
						for (let d of res?.data.data?.tableData) {
							timeList.push({timeSt: d.startTime, timeEn: d.endTime});
						}
						 this.timeList = timeList
				    })
			},
			//查询会员预约列表
			searchAppointmentList(flag) {
				let _this = this;
				this.$utils.requestFun('/stadium/laboratoryOrder/api/queryUserLaboratoryOrderList?route=curriculum',{
					token: getApp().globalData.token,
					isVisited: _this.isVisited,//是否只查询被邀请的预约单据 0否；1是
					orderStates:"0,10,20,30",
					page: _this.page,
					limit: _this.limit
				},'GET').then(res=> {
					// _this.appointmentList = res.data.data.data ? res.data.data.data : [];
					let appointmentData = _this.appointmentList;
					if(flag == 1) {
						_this.appointmentList = res.data.data.data && res.data.data.data.length > 0 ? res.data.data.data : [];
					}else {
						if(res.data.data.data && res.data.data.data.length > 0) {
							if(_this.page == 1) {
								appointmentData = [];
							}
							_this.classrooms = [];
							let appointmentList = res.data.data.data;
							for (let s of appointmentList) {
								console.log(s);
								if (!_this.classrooms.find(v=>v.laboratoryName === s.laboratoryName)) {
									_this.classrooms.push({laboratoryName: s.laboratoryName, laboratoryId: s.laboratoryId});
								}
							}
							_this.queryTimeList()
							if(_this.classrooms.length > 0){
								_this.queryTimeList(_this.classrooms[0].laboratoryId);
							}
							
							if(appointmentList.length < _this.limit) {
								_this.appointmentList = appointmentData.concat(appointmentList);
								_this.hasMoreData = false;
							}else {
								_this.appointmentList = appointmentData.concat(appointmentList);
								_this.hasMoreData = true;
								_this.page = _this.page++;
							}
						}else {
							if(_this.page == 1) {
								_this.appointmentList = [];
							}else {
								_this.appointmentList = appointmentData;
							}
						}
					}
				})
			},
			lower(e) {
				if (this.hasMoreData) {
					let page = this.page+1;
					this.page = page;
					this.searchAppointmentList();
				} else {
					uni.showToast({
						title: '没有更多数据',
						icon: 'none'
					})
				}
			},
			//取消订单
			cancleOrder(data) {
				let _this = this;
				uni.showModal({
				    title: '提示',
				    content: '是否确认取消订单？',
				    success: function (res) {
				        if (res.confirm) {
							_this.$utils.requestFun('/stadium/laboratoryOrder/api/memberCancelLaboratoryOrder?route=curriculum',{
								token: getApp().globalData.token,
								appId: getApp().globalData.appid,
								orderId: data.orderId 
							},'GET').then(res=> {
								console.log(res);
								uni.showToast({
									title: '取消成功！',
									success() {
										_this.scrollTop = 0;
										_this.page = 1;
										_this.appointmentList = [];
										_this.searchAppointmentList();
									}
								})
							});
				        } else if (res.cancel) {
				            console.log('用户点击取消');
				        }
				    }
				});
			}
		}
	}
</script>

<style lang='scss' scoped>
	/* 变为： 可选 灰，已约 橙，开放： #70CC33，已选： #2BD56F #19AD78 */
	.tipsGreen {
		background: #19AD78;
	}
	
	.moreIconHui {
		width: 22rpx;
		height: 24rpx;
		margin-top: 10rpx;
	}
	.container {
	
	 display: flex;
	
	 flex-direction: column;
	
	 align-items: center;
	
	}
	
	.input-group {
	
	 width: 100%;
	
	}
	
	.picker {
	
	 font-size: 16px;
	
	}
	.tipsOrange {
		background: #FF7633;
	}
	
	.tipsHui {
		background: #EBEBEB;
	}
	
	.tipsOpen {
		background: #70CC33;
	}
	
	.main {
		width: 100%;
		box-sizing: border-box;
		padding-bottom: 20rpx;
		border-bottom: 1px solid #DDDDDD;
		padding-bottom: 236rpx;
	}
	
	.mainL {
		margin-top: 25rpx;
		width: 90rpx;
	
	}
	
	.mainL-li {
		margin-top: 20rpx;
		padding-left: 5rpx;
		padding-right: 5rpx;
		font-weight: bold;
		color: #666666;
		text-align: center;
		width: 100rpx;
		height: 60rpx;
		border-right: 1rpx solid #fff;
		border-bottom: 1rpx solid #fff;
		line-height: 60rpx;
		display: inline-block;
		font-size: 28rpx;
	}
	
	
	.mainRUl {
		margin-left: 20rpx
	}
	
	.mainRUl-li {
		/* margin-top: 20rpx; */
		/* width: 80rpx; */
		height: 60rpx;
		/* border-radius: 5px; */
		border-bottom: 1rpx solid #fff;
		border-right: 2rpx solid #fff;
	}
	
	.mainRUl-li.mainRUl-li-spel {
		font-size: 26rpx;
		line-height: 60rpx;
		color: #666;
		font-weight: 700;
	}
	
	.class-table {
	    .table-wrapper {
	        width: 100%;
	        height: 100%;
	        overflow: auto;
	    }
	
	    .tabel-container {
	        /* margin: 7px; */
	
	        table {
	            table-layout: fixed;
	            width: 100%;
	            thead {
	                // background-color: #409EFF;
	                background-color: #18CCA7;
	
	                th {
	                    color: #fff;
	                    line-height: 17px;
	                    font-weight: normal;
	                }
	            }
	
	            tbody {
	                background-color: #f4f4f5;
	
	                // background-color: #fff;
	                td {
	                    // color: #677998;
	                    color: #333;
	                    line-height: 12px;
	                }
	            }
	
	            th {
	                width: 60px;
	                padding: 0px 2px;
	                font-size: 12px;
	                text-align: center;
	            }
	
	            td {
	                width: 60px;
	                /* padding: 5px 2px; */
	                font-size: 12px;
	                text-align: center;
	            }
	
	            tr td:first-child {
	                color: #333;
	
	                .period {
	                    font-size: 8px;
	                }
	            }
				 th:first-child {
				  position:sticky;
				  left:2px;
				  z-index:2;
				  background-color: #18CCA7;
				}
				td:first-child {
				  position:sticky;
				  left:2px;
				  z-index:2;
				  background-color: #f4f4f5;
				  border-right-color:#aaa;
				}
	        }
	    }
	}
</style>
<style>
	
	.moreAppointmentContainer {
		width: 100%;
		height: 100vh;
		overflow: scroll;
		background: #F6F6F6;
	}
	.txtOverFlow {
		overflow: hidden;
		text-overflow:ellipsis;
		white-space: nowrap;
	}
	.flex {
		display: flex;
	}
	.noDataTxt {
		color: #999999;
		text-align: center;
		margin: 100rpx 0;
		line-height: 200rpx;
	}
	.headNavBox {
		width: 100%;
		height: 80rpx;
		background: #FFFFFF;
	}
	.headNavItem {
		width: 33%;
		text-align: center;
		line-height: 74rpx;
	}
	.headNavTitle {
		font-size: 28rpx;
		color: #222222;
	}
	.activeTab {
		width: 46rpx;
		height: 6rpx;
		background-image: linear-gradient(to right, #1eaf7b , #c7e2ac);
		background-size: 100% 100%;
		background-repeat: no-repeat;
	}
	.appointmentItem {
		width: 93.5%;
		margin: 20rpx 24rpx;
		min-height: 160rpx;
		border-radius: 20rpx;
		background-color: #FFFFFF;
		padding-bottom: 10rpx;
	}
	.appointmentHead {
		width: 94%;
		height: 60rpx;
		margin: 0 20rpx;
		padding-top: 30rpx;
		border-bottom: 1px solid #EEEEEE;
	}
	.classRoomIcon {
		width: 25rpx;
		height: 25rpx;
		margin-right: 15rpx;
		margin-top: 6rpx;
	}
	.appointmentTit {
		font-size: 28rpx;
		font-weight: bold;
		color: #222222;
		width: 40%;
		text-align: left;
	}
	.appointmentTime {
		font-size: 26rpx;
		color: #222222;
		width: 30%;
		text-align: right;
	}
	.appointmentTimeBox {
		/* margin-bottom: 30rpx; */
		min-width: 80rpx;
		display: flow-root;
	}
	.appointmentTimeItem {
		width: 206rpx;
		height: 80rpx;
		line-height: 80rpx;
		background-color: #F6F6F6;
		border-radius: 10rpx;
		margin-top: 30rpx;
		margin-left: 20rpx;
		float: left;
		font-size: 28rpx;
		color: #222222;
		text-align: center;
	}
	.appointmentLocationBox {
		width: 94%;
		clear: both;
		margin: 0 20rpx;
		font-size: 28rpx;
		padding-top: 16rpx;
		color: #666666;
	}
	.locationIcon {
		width: 27rpx;
		height: 27rpx;
		margin-right: 13rpx;
		margin-top: 10rpx;
	}
	.locationTxt {
		width: 93%;
		margin-bottom: 20rpx;
	}
	.orderBtn {
		width: 140rpx;
		height: 60rpx;
		background: linear-gradient(90deg, #19AD78, #1EBE85);
		border-radius: 30rpx;
		text-align: center;
		font-size: 26rpx;
		color: #FFFFFF;
		margin-left: 77%;
		line-height: 60rpx;
	}
</style>
